//
// Created by HP on 2025/11/17.
//

#include "Travel.h"
#include <iostream>

Travel::Travel() : Model("travels") {
    this->onTime = "true";
    this->isEnd = "false";
    setTableDefault("userId", "INT NOT NULL");
    setTableDefault("transportationId", "INT NOT NULL");
    setTableDefault("onTime", "VARCHAR(10) NOT NULL DEFAULT 'true'");
    setTableDefault("isEnd", "VARCHAR(10) NOT NULL DEFAULT 'false'");
    setTableDefault("delayTime", "INT DEFAULT 0");
    setTableDefault("createdAt", "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP");
    setTableDefault("updatedAt", "DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP");
}

void Travel::bind(const std::string& key, const std::string& value) {
    if (!TableCols.count(key)) {
        std::cerr << "Error: Invalid key for travel bind" << std::endl;
        return;
    }
    if (TableCols[key].find("UNIQUE") == std::string::npos && key != "id") {
        std::cerr << "Error: Key is not unique for travel bind" << std::endl;
        return;
    }

    std::string selectSql = "SELECT * FROM " + TableName + " WHERE " + key + " = '" + Model::escapeSqlLiteral(value) + "';";
    std::vector<std::map<std::string, std::string>> result = DBConnector::getInstance()->query(selectSql);

    if (result.empty()) {
        std::cerr << "bind failed: SQL execution error or no result. SQL: " << selectSql << std::endl;
        return;
    }

    std::cout << "bind success. Table: " << TableName << std::endl;

    if (result.size() > 1) {
        std::cerr << "Error: Multiple travels found with the same " << key << std::endl;
        return;
    } else if (result.size() == 1) {
        this->id = result[0]["id"];
        this->userId = result[0]["userId"];
        this->transportationId = result[0]["transportationId"];
        this->onTime = result[0]["onTime"];
        this->isEnd = result[0]["isEnd"];
        this->delayTime = result[0].count("delayTime") > 0 ? result[0]["delayTime"] : "0";
        this->createdAt = result[0]["createdAt"];
        this->updatedAt = result[0]["updatedAt"];
    }
}

bool Travel::save() {
    std::string createTravelSql = "INSERT INTO " + TableName + " (userId, transportationId, onTime, isEnd";
    std::string valuesSql = " VALUES (" + userId + ", " + transportationId + ", '" 
                           + Model::escapeSqlLiteral(onTime) + "', '" 
                           + Model::escapeSqlLiteral(isEnd) + "'";
    
    if (!delayTime.empty() && delayTime != "0") {
        createTravelSql += ", delayTime";
        valuesSql += ", " + delayTime;
    }
    
    createTravelSql += ")";
    valuesSql += ");";
    
    std::string fullSql = createTravelSql + valuesSql;
    int executeCode = DBConnector::getInstance()->execute(fullSql);
    if (executeCode == -1) {
        std::cerr << "create travel failed: SQL execution error. SQL: " << fullSql << std::endl;
        return false;
    } else {
        std::cout << "create travel success. Table: " << TableName << std::endl;
        return true;
    }
}
